import { LANGUAGE_ZH, LANGUAGE_ZH_TD, LANGUAGE_EN } from './types';
import config from './config';

export class I18n {
  constructor(language) {
    this.language = language;
    let locale = {};
    Object.keys(config).forEach(keyName => {
      locale[keyName] = config[keyName][this.language];
    });
    this.locale = locale;
  }
  getLocale() {
    return this.locale;
  }
  getLocaleByName(name) {
    const locale = this.locale[name];
    return locale || name;
  }
  changeLanguage(language) {
    if (language != this.language) {
      this.language = language;

      let locale = {};
      Object.keys(config).forEach(keyName => {
        locale[keyName] = config[keyName][this.language];
      });
      this.locale = locale;
    }
    return this;
  }
  static getLanguageType = name => {
    if ( name.indexOf('en') > -1 ) {
      return LANGUAGE_EN;
    } else {
      switch (name.toLowerCase()) {
        case 'zh_cn':
        case 'zh-cn':
          return LANGUAGE_ZH;
        case 'zh_hk':
        case 'zh_tw':
        case 'zh-hk':
        case 'zh-tw':
        case 'zh_cmn_hant':
        case 'zh-cmn-hant':
        case 'zh_hant':
        case 'zh-hant':
        case 'zh-hant-hk':
        case 'zh-hant-tw':
        case 'zh_hant_hk':
        case 'zh_hant_tw':
          return LANGUAGE_ZH_TD;
        default:
          return LANGUAGE_ZH;
      }
    }
  };
}
